<?php
$ro=file_get_contents('php://input'); //得到前端的json文件
$user=json_decode($ro,true);   //转变成php数组
$id=$user['id'];
$name=$user['studentName'];   //取出对应数据
$account=$user['account'];
$password=$user['password'];
$gender=$user['gender'];
$class=$user['class'];
$schoolNumber=$user['schoolNumber'];
$flag=$user['flag'];


$link=new mysqli('127.0.0.1','root','root','books_manage_system','3306');
if($link->connect_error){
    die('连接失败'.$link->connect_error);
}else{
    //连接成功，设置编码格式
    mysqli_set_charset($link,'utf8');
    $link->query('SET NAMES UTF8');

    //存储数据
    $result=array();

    if($flag=='add'){
        $sql1="select * from users where account='$account'";
        $sql2="select * from users where schoolNumber='$schoolNumber'";
        $sql3="insert into users (id,account,password,studentName,class,schoolNumber,gender,status,borrowBookId) values (NULL,'$account','$password','$name','$class','$schoolNumber','$gender','0',NULL)";
        $re1=$link->query($sql1);
        $re2=$link->query($sql2);
        $num1=mysqli_num_rows($re1);
        $num2=mysqli_num_rows($re2);
        //是否重复
        if($num1>0 || $num2>0){
            $result['status']='fail';
            $result['message']='账号或学号重复';
        }else{
            $re3=$link->query($sql3);
            if($re3>0){
                 $result['status']='success';
                $result['message']='添加用户成功';
            }else{
                $result['status']='fail';
                $result['message']='添加用户失败';
            }
        }
        mysqli_free_result($re1);
        mysqli_free_result($re2);
    }else{
        $sql="select * from users where id='$id'";
        $sql1="update users set gender='$gender',password='$password',class='$class',studentName='$name' where id='$id'";
        $sql2="select * from users where schoolNumber='$schoolNumber'";
        $sql3="update users set gender='$gender',password='$password',class='$class',studentName='$name',schoolNumber='$schoolNumber' where id='$id'";
        $re=$link->query($sql);
        $num=mysqli_num_rows($re);
        if($num>0){
            $row=mysqli_fetch_assoc($re);
            if($row['schoolNumber']==$schoolNumber){
                    $re1=$link->query($sql1);
                    if($re1>0){
                        $result['status']='success';
                        $result['message']='修改成功';
                    }else{
                        $result['status']='fail';
                        $result['message']='修改失败';
                    }
            }else{
                $re2=$link->query($sql2);
                $num2=mysqli_num_rows($re2);
                if($num2>0){
                    $result['status']='fail';
                    $result['message']='学号重复';
                }else{
                    $re3=$link->query($sql3);
                    if($re3>0){
                        $result['status']='success';
                        $result['message']='修改成功';
                    }else{
                        $result['status']='fail';
                        $result['message']='修改失败';
                    }
                }
                mysqli_free_result($re2);
            }
        }else{
            $result['status']='fail';
            $result['message']='参数错误';
        }
        mysqli_free_result($re);
    }
//关闭数据库
$link->close();
die(json_encode($result));
}

?>